package com.example.mobilesafe.sw1221.home.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mobilesafe.sw1221.R;

import java.io.InputStream;
import java.lang.reflect.Type;

public class ArcProgressBar extends View {

    private Paint paint;
    private int    textColor;
    private float  textSize;
    private int    max;
    private int    progress;
    private boolean  isDisplayText;
    private String title;
    private Bitmap bmpTemp=null;
    private int degress;




    public ArcProgressBar(Context context) {

        this(context,null);

    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs) {

        this(context, attrs,0);
    }

    public ArcProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        degress=0;
        paint=new Paint();
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.XCRoundProgressBar);

        textColor=typedArray.getColor(R.styleable.XCRoundProgressBar_textColor, Color.RED);

        textSize=typedArray.getDimension(R.styleable.XCRoundProgressBar_textSize,19);

        max=typedArray.getInteger(R.styleable.XCRoundProgressBar_max,100);

        isDisplayText=typedArray.getBoolean(R.styleable.XCRoundProgressBar_textIsDisplay,true);
        typedArray.recycle();

    }

    @Override
    protected  void onDraw(Canvas canvas){
        super.onDraw(canvas);
        int width=getWidth();
        int height=getHeight();
        int centerX=getWidth()/2;
        int centerY=getHeight()/2;


        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas carvas = new Canvas (bitmap);

        bmpTemp= decodeCustomRes(getContext(),R.drawable.arc_bg);

        float detWidth=(float)width;
        float detHeight=(float)height;
        int   srcWidth=bmpTemp.getWidth();
        int   srcHeight=bmpTemp.getHeight();

        canvas.setDrawFilter(new PaintFlagsDrawFilter(0,Paint.ANTI_ALIAS_FLAG|
                Paint.FILTER_BITMAP_FLAG));

        Bitmap bmpbg = Bitmap.createScaledBitmap(bmpTemp, width, height, true);
        canvas.drawBitmap(bmpbg,0,0,null);


        Matrix martrixPrgress=new Matrix();
        martrixPrgress.postScale(detWidth/srcWidth,
                detHeight/srcWidth);
        bmpTemp=decodeCustomRes(getContext(),R.drawable.arc_progress);
        Bitmap bmpProgress = Bitmap.createBitmap
                (bmpTemp, 0, 0, srcWidth, srcHeight, martrixPrgress, true);

        degress=progress*270/max-270;

        canvas.save();
        canvas.rotate(degress,centerX,centerY);

        paint.setAntiAlias(true);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        canvas.drawBitmap(bmpProgress,0,0,paint);
        canvas.restore();

        if ((-degress)>=85){
            int posX=0;
            int posY=0;
            if ((-degress)>=270){
                posX=0;
                posY=0;

            }if ((-degress)>=225){
                posX=centerX;
                posY=0;

            }if ((-degress)>=180){
                posX=centerX;
                posY=0;

            }if ((-degress)>=138){
                posX=centerX;
                posY=0;

            }if ((-degress)>=85){
                posX=centerX;
                posY=centerY;
            }


            if ((-degress)>225){
                canvas.save();
                Bitmap dst = bitmap.createBitmap(bitmap, 0, 0, centerX, centerY);

                paint.setAntiAlias(true);

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));


                Bitmap src = bmpbg.createBitmap(bmpbg, 0, 0, centerX, centerY);
                canvas.drawBitmap(src,0,0,paint);
                canvas.restore();


                canvas.save();
                dst = bitmap.createBitmap(bitmap, centerX, 0, centerX, height);
                paint.setAntiAlias(true);

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
                src = bmpbg.createBitmap(bmpbg, centerX, 0, centerX, height);

                canvas.drawBitmap(src,centerX,0,paint);
                canvas.restore();


            }else {
                canvas.save();
                Bitmap dst = bitmap.createBitmap(bitmap, posX, posY, width - posX, height - posY);
                paint.setAntiAlias(true);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

                Bitmap src = bmpbg.createBitmap(bmpbg, posX, posY, width - posX, height - posY);
                canvas.drawBitmap(src,posX,posY,paint);
                canvas.restore();

            }

        }
        canvas.drawBitmap(bitmap,0,0,null);
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setTypeface(Typeface.DEFAULT.DEFAULT_BOLD);

        int percent=(int)(((float)progress/(float)max)*100);

        float textWidth=paint.measureText(percent+"%");
        if (isDisplayText&& percent!=0){

            canvas.drawText(percent+"%",centerX-textWidth/2,
                    centerY+textSize/2-25,paint);

            paint.setTextSize(textSize);
            textWidth=paint.measureText(title);
            canvas.drawText(title,centerX-textWidth/2,
                    height-textSize/2,paint);


        }


    }

    private Bitmap decodeCustomRes(Context c, int res) {
        InputStream is = c.getResources().openRawResource(res);

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=false;
        options.inSampleSize=1;

        Bitmap bmpmap = BitmapFactory.decodeStream(is, null, options);

        return bmpmap;


    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public boolean isDisplayText() {
        return isDisplayText;
    }

    public void setDisplayText(boolean displayText) {
        isDisplayText = displayText;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;

    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        if (max<0){

            throw  new IllegalArgumentException("max must be more than 0");

        }
        this.max = max;
    }

    public int getProgress() {

        return progress;
    }

    public void setProgress(int progress) {
        if (progress<0){

            throw  new IllegalArgumentException ("progress must be more than 0");

        }
        if (progress<max){

            this.progress=progress;

        }
        if (progress>max){

            this.progress = progress;
            postInvalidate();
        }

    }
}





